/*
 * Copyright (C) 2012 JiangHongTiao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.paris.core.persistence;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import weka.classifiers.Classifier;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
@Entity
public class ClassifierStatistics implements Serializable {

    private transient Logger log = Logger.getLogger(ClassifierStatistics.class);
    private transient PropertiesUtils pu = new PropertiesUtils(ClassifierStatistics.class);
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String name;
    private String classificationGroup;
    private Timestamp classificationTime;
    @Lob
    private byte[] classifier;
    @ManyToOne(fetch= FetchType.EAGER, cascade=CascadeType.PERSIST)
    private TrainSet trainSet;
    @OneToOne(fetch= FetchType.EAGER, cascade=CascadeType.PERSIST)
    private EvaluatorTestStatistics evaluatorTestStatistics;

    public ClassifierStatistics() {
    }

    public ClassifierStatistics(Classifier classifier, TrainSet trainSet,
            String classificationGroup, Timestamp classificationTime) {
        this.name = classifier.getClass().getSimpleName();
        this.trainSet = trainSet;
        this.classificationGroup = classificationGroup;
        this.classificationTime = classificationTime;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(classifier);
            oos.flush();
            oos.close();
            this.classifier = baos.toByteArray();
        } catch (IOException ex) {
            log.error("Cannot serialize Evaluation object to byteArray!");
        }
    }

    public Timestamp getClassificationTime() {
        return classificationTime;
    }

    public void setClassificationTime(Timestamp classificationTime) {
        this.classificationTime = classificationTime;
    }
    
    

    public String getClassificationGroup() {
        return classificationGroup;
    }

    public void setClassificationGroup(String classificationGroup) {
        this.classificationGroup = classificationGroup;
    }

    public byte[] getClassifier() {
        return classifier;
    }

    public void setClassifier(byte[] classifier) {
        this.classifier = classifier;
    }

    public EvaluatorTestStatistics getEvaluatorTestStatistics() {
        return evaluatorTestStatistics;
    }

    public void setEvaluatorTestStatistics(EvaluatorTestStatistics evaluatorTestStatistics) {
        this.evaluatorTestStatistics = evaluatorTestStatistics;
    }

    public TrainSet getTrainSet() {
        return trainSet;
    }

    public void setTrainSet(TrainSet trainSet) {
        this.trainSet = trainSet;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
